//
//  XSJMeVC.m
//  XSJ
//
//  Created by apple on 28.4.20.
//  Copyright © 2020 apple. All rights reserved.
//

#import "XSJMeVC.h"
#import "XSJMyHeadView.h"
#import "XSJMyOrderTableCell.h"
#import "XSJTitleTableCell.h"
#import "XSJCommonFunctionsTableCell.h"
#import "XSJTitleTableCell.h"
#import "XSJCancellationTableCell.h"
#import "XSJPersonalVC.h"
#import "XSJISawItVC.h"
#import "AppDelegate.h"
#import "http_user.h"
#import "SVProgressHUD.h"
#import "UserInfoModel.h"
#import "XSJCreateLiveRoomVC.h"


@interface XSJMeVC ()<UITableViewDataSource,UITableViewDelegate,XSJMyHeadViewDelegate,XSJMyOrderTableCellDelegate,XSJCommonFunctionsTableCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) XSJMyHeadView *headView;

@property (nonatomic, strong) UserInfoModel* userInfo;

@end

@implementation XSJMeVC

static NSString *const XSJMyOrderTableCellID = @"XSJMyOrderTableCellID";
static NSString *const XSJTitleTableCellID = @"XSJTitleTableCellID";
static NSString *const XSJCommonFunctionsTableCellID = @"XSJCommonFunctionsTableCellID";
static NSString *const XSJCancellationTableCellID = @"XSJCancellationTableCellID";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    //用户信息
   // [self loadData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)setupUI
{
    self.tableView.backgroundColor = TableViewBGColor;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //自动计算高度
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 50;
    
    [self.tableView registerClass:[XSJMyOrderTableCell class] forCellReuseIdentifier:XSJMyOrderTableCellID];
    [self.tableView registerClass:[XSJTitleTableCell class] forCellReuseIdentifier:XSJTitleTableCellID];
    [self.tableView registerClass:[XSJCommonFunctionsTableCell class] forCellReuseIdentifier:XSJCommonFunctionsTableCellID];
    [self.tableView registerClass:[XSJCancellationTableCell class] forCellReuseIdentifier:XSJCancellationTableCellID];
    
    self.headView = [[XSJMyHeadView alloc] initWithFrame:CGRectMake(0, 0, LL_ScreenWidth, 240)];
    _headView.delegate = self;
    self.tableView.tableHeaderView = self.headView;
}

//加载数据
//-(void)loadData
//{
//    ZWeakSelf
//    [http_my getUserBasic:[UserInfoModel getUID] viewedUserId:[UserInfoModel getUID] success:^(id responseObject)
//    {
//        [weakSelf loadData_ok:responseObject];
//
//    } failure:^(NSError *error) {
//
//        [SVProgressHUD showInfoWithStatus:error.domain];
//    }];
//}
//
////加载数据完成
//-(void)loadData_ok:(id)responseObject
//{
//    if (kObjectIsEmpty(responseObject))
//    {
//        return;
//    }
//
//    //jsonToModel
//    self.userInfo = [UserInfoModel mj_objectWithKeyValues:responseObject];
//    //刷新数据
//    self.headView.userInfo = self.userInfo;
//
//    [self.tableView reloadData];
//
//}

#pragma mark - Table view data source
//有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

//每个组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

//每行使用的Cell是什么
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        XSJMyOrderTableCell* cell = [tableView dequeueReusableCellWithIdentifier:XSJMyOrderTableCellID];
        if (cell == nil)
        {
            cell = [[XSJMyOrderTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XSJMyOrderTableCellID];
        }
        cell.delegate = self;
//        cell.userInfo = self.userInfo;
        return cell;
    }
    else if (indexPath.section==1)
    {
        XSJTitleTableCell* cell = [tableView dequeueReusableCellWithIdentifier:XSJTitleTableCellID];
        if (cell == nil)
        {
            cell = [[XSJTitleTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XSJTitleTableCellID];
        }
        return cell;
    }
    else if (indexPath.section==2)
    {
        XSJCommonFunctionsTableCell* cell = [tableView dequeueReusableCellWithIdentifier:XSJCommonFunctionsTableCellID];
        if (cell == nil)
        {
            cell = [[XSJCommonFunctionsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XSJCommonFunctionsTableCellID];
        }
        cell.delegate = self;
        return cell;
    }
    else if (indexPath.section==3)
    {
        XSJCancellationTableCell* cell = [tableView dequeueReusableCellWithIdentifier:XSJCancellationTableCellID];
        if (cell == nil)
        {
            cell = [[XSJCancellationTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XSJCancellationTableCellID];
        }
        return cell;
    }
    
    return nil;
}


//每行的高度是多少
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return 80.0f;
    }
    else if (indexPath.section==2)
    {
        return 160;
    }
    else if (indexPath.section==3)
    {
        return 60;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==1)
    {
        return 5;
    }
    return 0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *cview = [[UIView alloc]init];
    cview.backgroundColor = [UIColor clearColor];
    return cview;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==3)
    {
        //退出登录
        ZWeakSelf;
        NSString* message = [NSString stringWithFormat:@"确定退出软件？"];
        UIAlertController *alert =
        [UIAlertController alertControllerWithTitle:nil
                                            message:message
                                     preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 =
        [UIAlertAction actionWithTitle:@"取消"
                                 style:UIAlertActionStyleDefault
                               handler:nil];
        
        [alert addAction:action1];
        
        UIAlertAction *action =
        [UIAlertAction actionWithTitle:@"确定"
                                 style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * _Nonnull action) {
                                   
                                   [weakSelf toExit];
                               }];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
//    if (indexPath.section==2)
//    {
//        if (indexPath.row==0)
//        {
//            //谁看过我
//            XSJISawItVC* vc = [[XSJISawItVC alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//        else if (indexPath.row==1)
//        {
//            //礼物信息
//            YCHGiftInformationJXVC* vc = [[YCHGiftInformationJXVC alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
//        }
////        else if (indexPath.row==2)
////        {
////            //我的成就
////            YCHMyAchievementsVC* vc = [[YCHMyAchievementsVC alloc]init];
////            [self.navigationController pushViewController:vc animated:YES];
////        }
//        else if (indexPath.row==2)
//        {
//            //设置
//            YCHSetUpVC* vc = [[YCHSetUpVC alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//    }
}


//我的头部headview被点击 1:个人资料 2:好友 3:关注 4:粉丝 5:作品
- (void)XSJMyHeadViewDidClick:(int)type
{
    if (type==1)
    {
        //跳转到个人资料
        XSJPersonalVC* vc = [[XSJPersonalVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (type==2)
        {
            //跳转到好友
    //        YCHMyFriendJXVC* vc = [[YCHMyFriendJXVC alloc] init];
    //        [vc defaultshowAtIndex:2];
    //        [self.navigationController pushViewController:vc animated:YES];

        }
    else if (type==3)
    {
        //跳转到关注
//        YCHMyFriendJXVC* vc = [[YCHMyFriendJXVC alloc] init];
//        [vc defaultshowAtIndex:2];
//        [self.navigationController pushViewController:vc animated:YES];

    }
    else if (type==4)
    {
        //跳转到粉丝
//        YCHMyFriendJXVC* vc = [[YCHMyFriendJXVC alloc] init];
//        [vc defaultshowAtIndex:1];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (type==5)
    {
        //跳转到作品
//        YCHRechargeVC* vc = [[YCHRechargeVC alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
}

/**
 消息 cell被点击
 */
- (void)XSJMyOrderTableCellDidClick:(NSIndexPath*)indexPath
{
    if (indexPath.item==0)
    {
        //消息
//        YCHVIPVC* vc = [[YCHVIPVC alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.item==1)
    {
        //任务
//        YCHHallMyRoomVC* vc = [[YCHHallMyRoomVC alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.item==2)
    {
        //财产
//        YCHWalletVC* vc = [[YCHWalletVC alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.item==3)
    {
        //已购买
//        YCHMyFriendJXVC* vc = [[YCHMyFriendJXVC alloc] init];
//        [vc defaultshowAtIndex:0];
//        [self.navigationController pushViewController:vc animated:YES];
    }
}

/**
 我的直播 cell被点击
 */
- (void)XSJCommonFunctionsTableCellDidClick:(NSIndexPath*)indexPath
{
    if (indexPath.item==0)
    {
        //我的直播
        XSJCreateLiveRoomVC* vc = [[XSJCreateLiveRoomVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.item==1)
    {
        //我的动态
//        YCHHallMyRoomVC* vc = [[YCHHallMyRoomVC alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.item==2)
    {
        //我的房间
//        YCHWalletVC* vc = [[YCHWalletVC alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.item==3)
    {
        //我的团队
//        YCHMyFriendJXVC* vc = [[YCHMyFriendJXVC alloc] init];
//        [vc defaultshowAtIndex:0];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.item==4)
        {
            //我的公会
    //        YCHMyFriendJXVC* vc = [[YCHMyFriendJXVC alloc] init];
    //        [vc defaultshowAtIndex:0];
    //        [self.navigationController pushViewController:vc animated:YES];
        }
    else if (indexPath.item==5)
    {
        //历史记录
        XSJISawItVC* vc = [[XSJISawItVC alloc] init];
        //[vc defaultshowAtIndex:0];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.item==6)
        {
            //联系客服
    //        YCHMyFriendJXVC* vc = [[YCHMyFriendJXVC alloc] init];
    //        [vc defaultshowAtIndex:0];
    //        [self.navigationController pushViewController:vc animated:YES];
        }
    else if (indexPath.item==7)
        {
            //我的等级
    //        YCHMyFriendJXVC* vc = [[YCHMyFriendJXVC alloc] init];
    //        [vc defaultshowAtIndex:0];
    //        [self.navigationController pushViewController:vc animated:YES];
        }
}


-(void)toExit
{
    ZWeakSelf
    [http_user logout:nil success:^(id responseObject)
     {
        [weakSelf logout_ok:responseObject];
    } failure:^(NSError *error)
    {
        [SVProgressHUD showInfoWithStatus:error.domain];
    }];
}

-(void)logout_ok:(id)responseObject
{
    //归档清空
    [UserInfoModel removeUserInfo];
    AppDelegate* app = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    [app to_LoginVC];
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -LL_StatusBarHeight, LL_ScreenWidth, LL_ScreenHeight) style:UITableViewStylePlain];
        //设置数据源，注意必须实现对应的UITableViewDataSource协议
        _tableView.dataSource=self;
        //设置代理
        _tableView.delegate=self;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
